/*============================渲染路由============================================*/

import React, { Component } from 'react';
import { HashRouter, Redirect, Route } from 'react-router-dom';

import Error from '../../pages/Error';
import { inject, observer } from 'mobx-react';
@inject('lu')
@observer

 class LuY extends Component {
   util=(RouterList)=>{

       return RouterList.map((route,i)=>{
           // console.log(route.meta.require)
           // console.log(route.meta)

         if(route.meta && route.meta.require){//如果需要登录 并且登录了

             if(this.props.lu.user.isLogin){//如果用户登录了  获取登陆者权限 并与页面权限比较

                 const premissionL=this.props.lu.user.premission   //登陆者的权限

                 const roles=route.role  //页面的权限
                 if(roles.includes(premissionL)){  //如果页面的权限包含登陆者的权限

                     if(route.children){   //如果有子路由

                         return <Route  key={route.path} path={route.path} render={() =>
                             <route.component {...this.props}>
                                 {this.util(route.children)}
                                 <Route key={route.path} exact path={route.path} render={() => <Redirect to={route.redirect} />} />
                             </route.component>
                         }>
                         </Route>
                     }else{
                        return <Route key={route.path} exact path={route.path} component={route.component} />
                     }
                 }else{
                     /* 不具备页面权限，进入无权提示 */
                     // console.log(123123123)
                     return <Route key={route.path} exact path={route.path} component={Error} />;
                 }
             }else{

                 return <Redirect key={route.name} exact to='/Login'></Redirect>
             }
         }else if(route.redirect){
         //重定向redirect
         //     console.log(123123)
         //     console.log(route.redirect)
             return (<Route key={route.path} path={route.path} exact render={() => <Redirect to={route.redirect} />} />)
         }else{
             //啥都不要  直接进入界面
             return (<Route key={route.path} path={route.path} exact component={route.component} />)
         }
       })
   }

   // constructor(props) {
    //     super(props);
    //     this.state = {};
    // }

    render() {
        let {LuYou}=this.props
        return (
         <HashRouter>
             {this.util(LuYou)}
         </HashRouter>
        )
    }
}
export default LuY